package com.baomidou.mybatisplus.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * 云路供应链科技有限公司 版权所有 © Copyright 2019<br>
 *
 * @author tonglinpeng
 * @since Created in 2019/11/14
 */
public class Test {

    protected static final Logger logger = LoggerFactory.getLogger(AbstractTemplateEngine.class);

    public static Properties getParamFromProp(){
        try {
            InputStream resourceAsStream1 = Test.class.getClassLoader().getResourceAsStream("config.properties");
            BufferedReader br= new BufferedReader(new InputStreamReader(resourceAsStream1));
            Properties props = new Properties();
            props.load(br);
            return props;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        Properties paramFromProp = getParamFromProp();
        // 先清空文件夹
        if ("true".equals(paramFromProp.getProperty("cleanFlag"))) {
            logger.info("开始清空目录...... ");
            FileUtil.delete(paramFromProp.getProperty("outputDir"));
            logger.info("清空目录完毕...... ");
        }
        
        AutoGenerator mpg = new AutoGenerator();
        mpg.setSystem(paramFromProp.getProperty("system"));
        mpg.setModule(paramFromProp.getProperty("module"));
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(paramFromProp.getProperty("outputDir"));
        gc.setFileOverride(true);
        gc.setAuthor(paramFromProp.getProperty("author"));
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        gc.setIdType(IdType.AUTO);
        gc.setBaseColumnList(Boolean.parseBoolean(paramFromProp.getProperty("bseColumnList")));
        gc.setBaseTable(Boolean.parseBoolean(paramFromProp.getProperty("baseTable")));
        gc.setLimit(Integer.valueOf(paramFromProp.getProperty("limit")));
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(paramFromProp.getProperty("username"));
        dsc.setPassword(paramFromProp.getProperty("password"));
        dsc.setUrl(paramFromProp.getProperty("url"));
        dsc.setSchemaName(paramFromProp.getProperty("dbname"));
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(paramFromProp.getProperty("parent"));
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        String include = paramFromProp.getProperty("include");
        if (StringUtils.isNotEmpty(include)){
            strategy.setInclude(include.split(","));
        }
        String exclude = paramFromProp.getProperty("exclude");
        if (StringUtils.isNotEmpty(exclude)){
            strategy.setExclude(exclude.split(","));
        }

        mpg.setStrategy(strategy);

        mpg.execute();
    }
}
